/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.web.tutor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import com.feyaSoft.home.web.base.BaseController;
import com.feyaSoft.home.web.common.JsonMVCUtil;

/**
 * @author F Zhuang
 * @Nov 7, 2007
 * 
 * This file is used to .....
 */
public class LayoutTabFormController extends BaseController {
	// log file
	private final static Logger log = Logger
			.getLogger(LayoutTabFormController.class);

	/**
	 * Call by Ajax - here we need create a json object..
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();

		return null;
	}

	/**
	 * load data by id now. And return JSON data result to response
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView loadData(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// now query DB to get list of user for drop down list - hard code
		List<User> allUsers = new ArrayList<User>();
		User user1 = new User();
		user1.setId("10");
		user1.setName("Patrick Barnes");
		allUsers.add(user1);
		
		User user2 = new User();
		user2.setId("20");
		user2.setName("Fenqiang Zhuang");
		allUsers.add(user2);
		
		// call json format now...
		UserUtil.jsonResponse(allUsers, request, response);
		
		return null;
	}

	/**
	 * create health
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView createUser(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (request.getParameter("firstname") != null) {
			String firstname = (String) request.getParameter("firstname")
					.trim();
		}

		JSONObject errors = new JSONObject();

		try {
			String firstname = "";
			if (request.getParameter("firstname") != null) {
				firstname = (String) request.getParameter("firstname").trim();
			}

			JsonMVCUtil
					.jsonOkResponse("You enter first name is = " + firstname,
							request, response);

		} catch (Exception e) {
			log.error("createUser failed");
			JsonMVCUtil.jsonFailResponse("Internal Error, please try again",
					request, response);
		}

		return null;
	}

}
